/**
 * Created by hezhijie on 2016/11/23.
 */
var appVersionModule = angular.module("appVersion",[]);
appVersionModule.controller("appVersionCtrl", ['$scope',
    function ($scope) {
        $scope.init=function(){
            $scope.queryAppVersion();
            $scope.appVersion={};
            $("#update_from_target").bind("load", function () {
                if ($(window.frames["update_from_target"].document.body).find("pre").html()) {
                    var updateResult = JSON.parse($(window.frames["update_from_target"].document.body).find("pre").html());
                    if (updateResult.code != '10000') {
                        showMessage("更新失败," + updateResult.message)
                    } else {
                        $scope.queryAppVersion();
                        $scope.hideUpdate();
                        $scope.appVersion={};
                    }
                }
            })
        }
        $scope.queryAppVersion=function(){
            $get(basePath+"/rest/appVersion/_query",{},function(result){
                $scope.appVersionList=result.resultValue;
                $scope.$apply();
            })
        }


        $scope.showUpdate=function(index) {
            $("#query_app_version_div").hide();
            $("#update_div").show();
            if (typeof(index) != 'undefined') {
                $scope.appVersion=$scope.appVersionList[index];
                $scope.appVersion.forceUpdate=$scope.appVersion.forceUpdate+"";
            } else {
                $(".update_field.text").val("");
                $scope.appVersion={};
            }
        }
        $scope.deleteVersion=function(index){
            $scope.appVersion=$scope.appVersionList[index];
            if(confirm("确认删除版本【"+$scope.appVersion.version+"】？")){
                $post(basePath+"/rest/appVersion/"+$scope.appVersion.id+"/_delete",{},function(result){
                    $scope.queryAppVersion();
                    $scope.appVersion={};
                    //showMessage("删除成功")
                })
            }
        }
        $scope.saveUpdate=function() {
            var url;
            var type;
            if ($("[name='id']").val() == "") {
                url = basePath+"/rest/appVersion/?TOKEN=DDLBv2TvB5tP0izxP2lj3kTMp%2BQWM%2BxearvTySbk0T1LzDYRQEQlGLuCf7JqS570Ay6gxE0A6YnFUgQlmhou3Q==";
            } else {
                url = basePath+"/rest/appVersion/" + $("[name='id']").val()+"/_update?TOKEN=DDLBv2TvB5tP0izxP2lj3kTMp%2BQWM%2BxearvTySbk0T1LzDYRQEQlGLuCf7JqS570Ay6gxE0A6YnFUgQlmhou3Q==";
            }
            $("#update_form").attr("action", url).submit();
        }
        $scope.hideUpdate=function() {
            $("#query_app_version_div").show();
            $("#update_div").hide();
        }

        $scope.init();
    }
]);
